//
//  HadLogin_ViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HadLogin_ViewController.h"
#import "BaseTableView.h"

#import "EMSDK.h"



@interface HadLogin_ViewController ()<UIImagePickerControllerDelegate>

@property(nonatomic, strong) UIImageView *headerImageView;
@property(nonatomic, strong) UITextField *userNickName;
@property(nonatomic, strong) UITextField *sayFeeling;
@property(nonatomic, strong) UIImagePickerController *pickerController;
@property(nonatomic, strong) UserInfo * userinfoModel;

@end

@implementation HadLogin_ViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    
    if([EMClient sharedClient].isLoggedIn){
        
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        
        
        NSString *userName = [[EMClient sharedClient] currentUsername];
        NSString *file = [path stringByAppendingPathComponent:userName];
        
        NSData *imageData = [NSData dataWithContentsOfFile:file];
        
        if (imageData == NULL)
        {
            
            self.headerImageView.image = [UIImage imageNamed:@"headerLogin.png"];
            
        }
        else
        {
            
            self.headerImageView.image = [UIImage imageWithData:imageData];
        }
        
        
        
    }else
    {
        self.headerImageView.image = [UIImage imageNamed:@"defaultIcon@3x.png"];
    }
    
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
   // 页面配置
    [self config];
    
    [self createSubs];
}



-(void)config{
    self.navigationItem.title = @"个人信息";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor blackColor]}];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(backToLogin)];
    
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    backImage.image = [UIImage imageNamed:@"infoImage.jpeg"];
    backImage.alpha = 0.5;
    [self.view addSubview:backImage];
    
//    self.userinfoModel = [[UserInfo alloc]init];

}

-(void)backToLogin{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


-(void)createSubs{
    
    // 头像
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH / 2 - SPACED * 4, SCREENHEIGHT / 10, SPACED * 8, SPACED * 8)];
    
    self.headerImageView.image = [UIImage imageNamed:@"headerLogin.png"];
    [self.view addSubview:_headerImageView];
    UILongPressGestureRecognizer *longHeaderPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    self.headerImageView.userInteractionEnabled = YES;
    [self.headerImageView addGestureRecognizer:longHeaderPress];
    
    
    
    
    // 昵称
    
    
}

-(void)longPress:(UILongPressGestureRecognizer *)longPress{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"改变头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc]init];
        imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerVC.delegate = self;
        imagePickerVC.allowsEditing = YES;

        
        
    }];
    
    UIAlertAction *chooseFromAlbum = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc]init];
        // 设置资源来源
        imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickerVC.videoQuality = UIImagePickerControllerQualityTypeMedium;
        imagePickerVC.delegate = self;
        // 允许照片进入编辑模式
        imagePickerVC.allowsEditing = YES;
        [self presentViewController:imagePickerVC animated:YES completion:nil];
        
        
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:camera];
    [alertController addAction:chooseFromAlbum];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:YES completion:nil];
    
    
}

// 选择图片成功调用此方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    self.headerImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];

    
    
    UIImage *image =[info objectForKey:UIImagePickerControllerEditedImage];
    
    NSData *dataImage = UIImageJPEGRepresentation(image, 1.0f);
    
    //给model赋值
    
    
  
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    
    NSString *userName = [[EMClient sharedClient] currentUsername];
    
    NSString *file = [path stringByAppendingPathComponent:userName];
    [dataImage writeToFile:file atomically:YES];
//    NSLog(@"%@", file);
    
    
    EMImageMessageBody *body = [[EMImageMessageBody alloc]init];
    NSString *from = [[EMClient sharedClient] currentUsername];
    
    //生成Message
    EMMessage *message = [[EMMessage alloc]initWithConversationID:@"222222" from:from to:@"222222" body:body ext:nil];
    message.chatType = EMChatTypeChat;// 设置为单聊消息
    //message.chatType = EMChatTypeGroupChat;// 设置为群聊消息
    //message.chatType = EMChatTypeChatRoom;// 设置为聊天室消息
    
    [[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
        
    } completion:^(EMMessage *message, EMError *error) {
        
    }];
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
